package com.imooc.chart.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class YouTubeDownloader {

    public static void main(String[] args) {
        String filePath = "/Users/dengwubo/IdeaProjects/boob/common/src/main/resources/file.txt";
        String outputDirectory = "/Users/dengwubo/Movies/sucai";
        int numberOfThreads = 16;

        ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads);

        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath))))) {
            String line;
            while ((line = br.readLine()) != null && !line.isEmpty()) {
                String finalLine = line;
                Runnable downloadTask = () -> downloadVideo(finalLine, outputDirectory);
                executorService.submit(downloadTask);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 关闭线程池
        executorService.shutdown();
    }

    private static void downloadVideo(String videoUrl, String outputDirectory) {
        try {
            String proxyCommand = "export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890";
            String downloadCommand = String.format("/Users/dengwubo/Movies/sucai/yt-dlp_macos -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]' -o \"%s/%%(title)s.%%(ext)s\" \"%s\"", outputDirectory, videoUrl);
            String command = proxyCommand + " && " + downloadCommand;

            ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", command);
            processBuilder.redirectErrorStream(true); // 合并标准输出和标准错误流

            Process process = processBuilder.start();

            // 读取 yt-dlp 输出，获取下载进度信息
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    // 在这里解析 yt-dlp 输出，查找下载进度信息，例如百分比、已下载大小等
                    // 然后根据需要更新你的下载进度显示
                    System.out.println(line);
                }
            }

            // 等待命令执行完成
            int exitCode = process.waitFor();
            if (exitCode == 0) {
                System.out.println("Download successful for: " + videoUrl);
            } else {
                System.err.println("Download failed for: " + videoUrl);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
    private static String convertIntervalToTime(long interval) {
        long hours = interval / (1000 * 60 * 60);
        long minutes = (interval % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (interval % (1000 * 60)) / 1000;

        return String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }
}
